package com.hongxun.budget.ui.activity.tk;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;

import com.hongxun.budget.R;
import com.hongxun.budget.greendao.DDeviceOldEntityDao;
import com.hongxun.budget.ui.activity.BaseListActivity;
import com.hongxun.budget.utils.DBUtils;
import com.hongxun.budget.utils.GreenDaoManager;
import com.hongxun.budget.utils.SPUtils;
import com.hongxun.greenentity.tk.BDeviceEntity;
import com.hongxun.greenentity.tk.DDeviceOldEntity;
import com.jessewu.library.SuperAdapter;
import com.jessewu.library.view.ViewHolder;

import org.greenrobot.greendao.query.QueryBuilder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class UserOldDeviceActivity extends BaseListActivity {

    List<DDeviceOldEntity> datas = new ArrayList<>();
    SuperAdapter<DDeviceOldEntity> adapter;
    @BindView(R.id.search_info)
    EditText searchInfo;

    @Override
    public int getMyListLayoutId() {
        return R.layout.activity_user_old_device;
    }

    @Override
    public SuperAdapter getAdapter() {
        return adapter = new SuperAdapter<DDeviceOldEntity>(R.layout.content_userold_device) {
            @Override
            public void bindView(ViewHolder viewHolder, final DDeviceOldEntity dDeviceOldEntity, final int i) {
                ((TextView) viewHolder.getView(R.id.name)).setText(dDeviceOldEntity.getDRName());
                ((TextView) viewHolder.getView(R.id.type)).setText(dDeviceOldEntity.getDLabel());
                ((TextView) viewHolder.getView(R.id.parameter)).setText(dDeviceOldEntity.getDRParameter());
                ((TextView) viewHolder.getView(R.id.mid_type)).setText(dDeviceOldEntity.getDRMidtype());
                ((TextView) viewHolder.getView(R.id.option)).setText(DBUtils.getBDataDictionaryEntity("opinion_type", dDeviceOldEntity.getDROpinion()).getDValue());
            }
        };
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        adapter.setOnItemClickListener(new SuperAdapter.OnItemClickListener<DDeviceOldEntity>() {
            @Override
            public void onClick(int i, DDeviceOldEntity d) {
                BDeviceEntity b = new BDeviceEntity();
                b.setGuid(d.getGuid());
                b.setDBigtype(d.getDRBigtype());
                b.setDParameter(d.getDRParameter());
                b.setDCode(d.getDRCode());
                b.setDLabel(d.getDLabel());
                b.setDMidtype(d.getDRMidtype());
                b.setDName("(利旧)" + d.getDRName());
                b.setDPrice(0.0);
                b.setDVoltage(d.getDRVoltage());
                b.setDUnit("利旧");
                Intent intent = new Intent();
                intent.putExtra("device", b);
                setResult(Activity.RESULT_OK, intent);
                finish();
            }
        });
        adapter.setData(datas);
        adapter.setEmptyDataView(R.layout.view_empty);
        getData();
    }

    void getData() {
        String search = searchInfo.getText().toString();
        QueryBuilder<DDeviceOldEntity> builder = GreenDaoManager.getInstance().getmDaoSession().getDDeviceOldEntityDao().queryBuilder();
        List<DDeviceOldEntity> list = builder.where(DDeviceOldEntityDao.Properties.DRDatastate.notEq("删除"), DDeviceOldEntityDao.Properties.DRWork.eq("拆除")
                , DDeviceOldEntityDao.Properties.DROpinion.eq("10"), DDeviceOldEntityDao.Properties.PGuid.eq(SPUtils.get(context, "project_guid", "")),
                builder.or(DDeviceOldEntityDao.Properties.DRName.like("%" + search + "%")
                        , DDeviceOldEntityDao.Properties.DRParameter.like("%" + search + "%"))).list();
        datas.addAll(list);
        adapter.notifyDataSetChanged();
    }

    @OnClick(R.id.search_button)
    public void onViewClicked() {
        getData();
    }
}
